﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// Ajax操作结果 
    /// </summary>
    public class AjaxResult
    {
        #region 构造函数
        /// <summary>
        /// 初始化一个Ajax操作结果 <see cref="AjaxResult"/> 的新实例
        /// </summary>
        public AjaxResult()
            : this(AjaxResultType.Success)
        { }

        /// <summary>
        /// 初始化一个Ajax操作结果 <see cref="AjaxResult"/> 的新实例
        /// </summary>
        /// <param name="type">Ajax结果类型</param>
        public AjaxResult(AjaxResultType type)
            : this(type, null)
        { }

        /// <summary>
        /// 初始化一个Ajax操作结果 <see cref="AjaxResult"/> 的新实例
        /// </summary>
        /// <param name="type">Ajax结果类型</param>
        /// <param name="content">消息内容</param>
        public AjaxResult(AjaxResultType type, string content)
            : this(type, content, null)
        { }

        /// <summary>
        /// 初始化一个Ajax操作结果 <see cref="AjaxResult"/> 的新实例
        /// </summary>
        /// <param name="type">Ajax结果类型</param>
        /// <param name="content">消息内容</param>
        /// <param name="data">返回数据</param>
        public AjaxResult(AjaxResultType type, string content, object data)
        {
            Type = type;
            Content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            Data = data;
        }

        #endregion

        #region 属性
        /// <summary>
        /// 获取或设置 Ajax结果类型
        /// </summary>
        public AjaxResultType Type { get; set; }

        #region 获取或设置 消息内容
        private string content;
        /// <summary>
        /// 获取或设置 消息内容
        /// </summary>
        public string Content
        {
            get { return content ?? Type.ToDescription(); }
            set { content = value; }
        }
        #endregion

        /// <summary>
        /// 获取或设置 返回数据
        /// </summary>
        public object Data { get; set; }

        /// <summary>
        /// 获取 是否成功
        /// </summary>
        public bool Succeeded => Type == AjaxResultType.Success;

        #endregion

        #region 200        
        /// <summary>
        /// 成功的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Success(string content = "")
        {
            return Success(null, content);
        }
        
        /// <summary>
        /// 成功的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Success(object data, string content = "")
        {
            var type = AjaxResultType.Success;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }
        #endregion

        #region 400        
        /// <summary>
        /// 错误的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Error(string content = "")
        {
            return Error(null, content);
        }
        
        /// <summary>
        /// 错误的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Error(object data, string content = "")
        {
            var type = AjaxResultType.Error;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }

        #endregion

        #region 401        
        /// <summary>
        /// 未登录的操作结果：
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Unauthorized(string content = "")
        {
            return Unauthorized(null, content);
        }
        
        /// <summary>
        /// 未登录的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Unauthorized(object data, string content = "")
        {
            var type = AjaxResultType.Unauthorized;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }

        #endregion

        #region 403                
        /// <summary>
        /// 已登录但权限不足的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Forbidden(string content = "")
        {
            return Forbidden(null, content);
        }

        /// <summary>
        /// 已登录但权限不足的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Forbidden(object data, string content = "")
        {
            var type = AjaxResultType.Forbidden;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }
        #endregion

        #region 404
        /// <summary>
        /// 资源未找到的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult NotFound(string content = "")
        {
            return NotFound(null, content);
        }

        /// <summary>
        /// 资源未找到的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult NotFound(object data, string content = "")
        {
            var type = AjaxResultType.NotFound;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }
        #endregion

        #region 405        
        /// <summary>
        /// 不允许的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult NotAllowed(string content = "")
        {
            return NotAllowed(null, content);
        }
        
        /// <summary>
        /// 不允许的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult NotAllowed(object data, string content = "")
        {
            var type = AjaxResultType.NotAllowed;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }
        #endregion

        #region 415
        /// <summary>
        /// 不支持的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Unsupported(string content = "")
        {
            return Unsupported(null, content);
        }
        
        /// <summary>
        /// 不支持的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Unsupported(object data, string content = "")
        {
            var type = AjaxResultType.Unsupported;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }

        #endregion

        #region 423
        /// <summary>
        /// 资源被锁定的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Locked(string content = "")
        {
            return Locked(null, content);
        }
        
        /// <summary>
        /// 资源被锁定的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult Locked(object data, string content = "")
        {
            var type = AjaxResultType.Locked;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }

        #endregion

        #region 500
        /// <summary>
        /// 服务器内部错误的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult InternalError(string content = "")
        {
            return InternalError(null, content);
        }
        
        /// <summary>
        /// 服务器内部错误的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult InternalError(object data, string content = "")
        {
            var type = AjaxResultType.InternalError;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }

        #endregion

        #region 501
        /// <summary>
        /// 功能未实现的操作结果
        /// </summary>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult NotImplemented(string content = "")
        {
            return NotImplemented(null, content);
        }
        
        /// <summary>
        /// 功能未实现的操作结果
        /// </summary>
        /// <param name="data">返回数据</param>
        /// <param name="content">消息内容</param>
        /// <returns>Ajax操作结果</returns>
        public static AjaxResult NotImplemented(object data, string content = "")
        {
            var type = AjaxResultType.NotImplemented;
            content = content.IsMissing() ? I18N.T(type.ToDescription()) : content;
            return new AjaxResult() { Type = type, Content = content, Data = data };
        }
        #endregion

    }
}